import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class RefreshComponentController {
  /// 构造函数
  /// [initialRefresh] 是否在初始化完成时刷新
  RefreshComponentController({bool initialRefresh = true}) {
    refreshController = RefreshController(initialRefresh: initialRefresh);
  }

  /// 刷新控制器
  late final RefreshController refreshController;

  /// Header
  Widget classicHeader() {
    return const ClassicHeader(
      idleText: '下拉刷新',
      releaseText: "松开开始刷新数据",
      completeText: '刷新完成',
      refreshingText: '刷新中...',
    );
  }

  Widget classicFooter() {
    return const ClassicFooter(
      idleText: '上拉加载',
      loadingText: '加载中...',
      canLoadingText: '松开开始加载数据',
      noDataText: '暂无更多数据',
    );
  }

  // = ClassicHeader(
  //   idleText: tr.common.refreshPullIdle,
  //   releaseText: tr.common.refreshPullRelease,
  //   completeText: tr.common.refreshPullComplete,
  //   refreshingText: tr.common.refreshPullRefreshing,
  // );

  /// Footer
  // Widget classicFooter = ClassicFooter(
  //   idleText: tr.common.refreshUpIdle,
  //   loadingText: tr.common.refreshUpLoading,
  //   canLoadingText: tr.common.refreshUpCanLoading,
  //   noDataText: tr.common.refreshUpNoData,
  // );

  /// after data returned,set the footer state to idle
  void loadComplete() {
    refreshController.loadComplete();
  }

  /// If catchError happen,you may call loadFailed indicate fetch data from network failed
  void loadFailed() {
    refreshController.loadFailed();
  }

  /// load more success without error,but no data returned
  void loadNoData() {
    refreshController.loadNoData();
  }

  /// request complete,the header will enter complete state,
  /// resetFooterState : it will set the footer state from noData to idle
  void refreshCompleted() {
    refreshController.refreshCompleted();
  }

  /// request failed,the header display failed state
  void refreshFailed() {
    refreshController.refreshFailed();
  }

  /// not show success or failed, it will set header state to idle and spring back at once
  void refreshToIdle() {
    refreshController.refreshToIdle();
  }

  /// make the header enter refreshing state,and callback onRefresh
  void requestRefresh({
    bool needMove = true,
    bool needCallback = true,
    Duration duration = const Duration(milliseconds: 500),
    Curve curve = Curves.linear,
  }) {
    refreshController.requestRefresh(
      needMove: needMove,
      needCallback: needCallback,
      duration: duration,
      curve: curve,
    );
  }

  /// 自定义刷新控制器
  /// [initialRefresh] 是否初始化完成调取刷新方法
  /// [initialRefreshStatus] 刷新模式默状态
  /// [initialLoadStatus] 加载模式默状态
  void createController({
    bool initialRefresh = true,
    RefreshStatus? initialRefreshStatus,
    LoadStatus? initialLoadStatus,
  }) {
    refreshController = RefreshController(
      initialRefresh: initialRefresh,
      initialRefreshStatus: initialRefreshStatus,
      initialLoadStatus: initialLoadStatus,
    );
  }

  // /// 自定义Header - Classic
  // void createClassicHeader({
  //   String idleText = '下拉刷新',
  //   String releaseText = '松开开始刷新数据',
  //   String completeText = '刷新完成',
  //   String refreshingText = '刷新中...',
  //   RefreshStyle refreshStyle = RefreshStyle.Follow,
  //   double height = 60.0,
  //   Duration completeDuration = const Duration(milliseconds: 600),
  //   TextStyle textStyle = const TextStyle(color: Colors.grey),
  //   Widget? canTwoLevelIcon,
  //   Widget? twoLevelView,
  //   String? canTwoLevelText,
  //   String? failedText,
  //   IconPosition iconPos = IconPosition.left,
  //   double spacing = 15.0,
  //   Widget? refreshingIcon,
  //   Widget? failedIcon = const Icon(Icons.error, color: Colors.grey),
  //   Widget? completeIcon = const Icon(Icons.done, color: Colors.grey),
  //   Widget? idleIcon = const Icon(Icons.arrow_downward, color: Colors.grey),
  //   Widget? releaseIcon = const Icon(Icons.refresh, color: Colors.grey),
  // }) {
  //   classicHeader = ClassicHeader(
  //     idleText: idleText,
  //     releaseText: releaseText,
  //     completeText: completeText,
  //     refreshingText: refreshingText,
  //     refreshStyle: refreshStyle,
  //     height: height,
  //     completeDuration: completeDuration,
  //     textStyle: textStyle,
  //     canTwoLevelIcon: canTwoLevelIcon,
  //     twoLevelView: twoLevelView,
  //     canTwoLevelText: canTwoLevelText,
  //     failedText: failedText,
  //     iconPos: iconPos,
  //     spacing: spacing,
  //     refreshingIcon: refreshingIcon,
  //     failedIcon: failedIcon,
  //     completeIcon: completeIcon,
  //     idleIcon: idleIcon,
  //     releaseIcon: releaseIcon,
  //   );
  // }

  // /// 自定义Footer - Classic
  // void createClassicFooter({
  //   String idleText = '上拉加载',
  //   String loadingText = '加载中...',
  //   String canLoadingText = '松开开始加载数据',
  //   String noDataText = '暂无更多数据',
  //   LoadStyle loadStyle = LoadStyle.ShowAlways,
  //   double height = 60.0,
  //   TextStyle textStyle = const TextStyle(color: Colors.grey),
  //   Widget? noMoreIcon,
  //   String? failedText,
  //   Widget? failedIcon = const Icon(Icons.error, color: Colors.grey),
  //   IconPosition iconPos = IconPosition.left,
  //   double spacing = 15.0,
  //   Duration completeDuration = const Duration(milliseconds: 300),
  //   Widget? loadingIcon,
  //   Widget? canLoadingIcon = const Icon(Icons.autorenew, color: Colors.grey),
  //   Widget? idleIcon = const Icon(Icons.arrow_upward, color: Colors.grey),
  // }) {
  //   classicFooter = ClassicFooter(
  //     idleText: idleText,
  //     loadingText: loadingText,
  //     canLoadingText: canLoadingText,
  //     noDataText: noDataText,
  //     loadStyle: loadStyle,
  //     height: height,
  //     textStyle: textStyle,
  //     noMoreIcon: noMoreIcon,
  //     failedText: failedText,
  //     failedIcon: failedIcon,
  //     iconPos: iconPos,
  //     spacing: spacing,
  //     completeDuration: completeDuration,
  //     loadingIcon: loadingIcon,
  //     canLoadingIcon: canLoadingIcon,
  //     idleIcon: idleIcon,
  //   );
  // }
}
